package my.edu.model.bills.bound.controller;

import cn.hutool.json.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import my.edu.common.domain.PageDTO;
import my.edu.common.domain.Response;
import my.edu.common.validate.groups.Insert;
import my.edu.common.validate.groups.Purchase;
import my.edu.constant.BoundBusinessType;
import my.edu.model.bills.bound.entity.dto.OutboundDTO;
import my.edu.model.bills.bound.entity.dto.OutboundOrderSearchDTO;
import my.edu.model.bills.bound.service.IOtherOutboundOrderService;
import my.edu.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/otherOutbound")
@RestController
@Tag(name = "其他出库管理")
public class OtherOutboundController {

    @Autowired
    private IOtherOutboundOrderService otherOutboundOrderService;

    @Autowired
    private BoundBusinessType businessType;

    @PutMapping
    @RequiresPermissions("otherOutbound:save")
    @Operation(summary = "新增")
    public Response addPurchaseOrder(@RequestBody @Validated(value = {Insert.class}) OutboundDTO outboundDTO) {
        otherOutboundOrderService.saveNew(outboundDTO);
        return Response.success();
    }

    @PostMapping("/update")
    @RequiresPermissions("otherOutbound:update")
    @Operation(summary = "修改")
    public Response updatePurchaseOrder(@RequestBody @Validated OutboundDTO outboundDTO) {
        otherOutboundOrderService.updateByDTO(outboundDTO);
        return Response.success();
    }

    @DeleteMapping("/deleteBatch")
    @RequiresPermissions("otherOutbound:del")
    @Operation(summary = "批量删除")
    public Response delSalesOrder(String ids) {
        List<Long> longs = StringUtils.strToLongList(ids);
        otherOutboundOrderService.removeOrder(longs);
        return Response.success();
    }

    @DeleteMapping("/delete")
    @RequiresPermissions("otherOutbound:del")
    @Operation(summary = "删除")
    public Response delete(Long id) {
        otherOutboundOrderService.removeOrder(List.of(id));
        return Response.success();
    }

    @PostMapping("/batchSetStatus")
    @RequiresPermissions(value = {"otherOutbound:audit"})
    @Operation(summary = "更新审核状态")
    public Response batchSetStatus(@RequestBody JSONObject jsonObject){
        int status = jsonObject.getInt("status");
        String ids = jsonObject.getStr("ids");
        List<Long> longs = StringUtils.strToLongList(ids);
        if(status==1){
            otherOutboundOrderService.auditOrder(longs);
        } else if (status==0) {
            otherOutboundOrderService.rauditOrder(longs);
        } else{
            return Response.error("状态值错误");
        }
        return Response.success();
    }

    @GetMapping("/list")
    @RequiresPermissions("otherOutbound:get")
    @Operation(summary = "查询订单")
    public Response getSalesOrder(@ModelAttribute @Validated PageDTO page,
                                  @ModelAttribute OutboundOrderSearchDTO outboundOrderSearchDTO) {
        return Response.success(otherOutboundOrderService.getOrderList(page, outboundOrderSearchDTO));
    }

    @GetMapping("/one")
    @RequiresPermissions("otherOutbound:get")
    @Operation(summary = "根据id查询订单")
    public Response getSalesOrder( @NotNull(message = "id不能为空") Long id) {
        return Response.success(otherOutboundOrderService.getOrderOne(id));
    }

    @GetMapping("/outboundBusinessType")
    @RequiresPermissions("otherOutbound:get")
    @Operation(summary = "获取仓库业务类型")
    public Response getBusinessType() {
        return Response.success(businessType.getOutboundBusinessType());
    }

}
